package com.hsnn.medstgmini.complain.controller.dispatch;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.complain.model.SysComplain;
import com.hsnn.medstgmini.complain.service.SysComplainAttachmentManager;
import com.hsnn.medstgmini.complain.service.SysComplainDelcompManager;
import com.hsnn.medstgmini.complain.service.SysComplainHospManager;
import com.hsnn.medstgmini.complain.service.SysComplainManager;
import com.hsnn.medstgmini.complain.service.SysComplainProdManager;
import com.hsnn.medstgmini.complain.service.SysComplainProdcompManager;
import com.hsnn.medstgmini.complain.service.SysComplainProjManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 配送企业 申投诉控制器
 * @author 韩守松
 * @date 2015年9月9日
 */
@Controller
@RequestMapping("/sysComplainDis")
public class SysComplainDisController {
private static final Logger log = Logger.getLogger(SysComplainDisController.class);
@Autowired
private SysComplainManager sysComplainManager ;

@Autowired
private SysComplainProjManager sysComplainProjManager;

@Autowired
private SysComplainHospManager sysComplainHospManager;

@Autowired
private SysComplainDelcompManager sysComplainDelcompManager;

@Autowired
private SysComplainProdcompManager sysComplainProdcompManager;

@Autowired
private SysComplainProdManager sysComplainProdManager;

@Autowired
private SysComplainAttachmentManager sysComplainAttachmentManager;

@Autowired
private HttpServletRequest request;

/**
 * @category  跳转到生产企业 申投诉列表页面
 * @author 韩守松
 * @date   2015年9月8日
 * @param  @return
 */
@RequestMapping("/toSysComplainListJsp")
public String toSysComplainListJsp(){
	return "/complain/dispatching/list";
}

@RequestMapping("/getSysComplainDataList")
@ResponseBody
public Pagination getSysComplainDataList(SysComplain sysComplain){
	Pagination page = new Pagination(request);
	try{
	SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
	sysComplain.setComplainDelcomp(user.getId());
	page=sysComplainDelcompManager.getSysComplainDataList(sysComplain);
	page.setSuccess(true);
	}catch(Exception e){
		log.equals("生产企业申投诉信息获取失败");
		page.setSuccess(false);
		e.printStackTrace();
	}
	return page;
}

/**
 * @category  跳转到生产企业 申投诉详情页面
 * @author 韩守松
 * @date   2015年9月8日
 * @param  @return
 */
@SuppressWarnings("rawtypes")
@RequestMapping("/toSysComplainDetailJsp")
public String toSysComplainDetailJsp(String complainId){
	List list =  sysComplainDelcompManager.getSysComplainById(complainId);
	request.setAttribute("list", list.get(0));
	List listProj = sysComplainProjManager.getSysComplainProjById(complainId);
	request.setAttribute("listProj", listProj);
	List listHosp = sysComplainHospManager.getSysComplainHospById(complainId);
	request.setAttribute("listHosp", listHosp);
	List listProdcomp = sysComplainProdcompManager.getSysComplainProdcompById(complainId);
	request.setAttribute("listProdcomp", listProdcomp);
	List listDelcomp = sysComplainDelcompManager.getSysComplainDelcompById(complainId);
	request.setAttribute("listDelcomp", listDelcomp);
	List listProd = sysComplainProdManager.getSysComplainProdById(complainId);
	request.setAttribute("listProd", listProd);
	List listAttachment = sysComplainAttachmentManager.getSysComplainById(complainId);
	request.setAttribute("listAttachment", listAttachment);
	return "/complain/dispatching/detail";
}
/**
 * @category  跳转到配送企业 申投诉新增页面
 * @author 韩守松
 * @date   2015年9月8日
 * @param  @return
 */
@RequestMapping("/toSysComplainAddJsp")
public String toSysComplainAddJsp(String complainType){
	if("1".equals(complainType)){
		request.setAttribute("complainTypeName", "投诉");
	}
	if("2".equals(complainType)){
		request.setAttribute("complainTypeName", "申诉");
	}
	SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
	request.setAttribute("prodName", user.getName());
	request.setAttribute("prodCompId", user.getId());
	request.setAttribute("typeName", user.getTypeName());
	request.setAttribute("type", user.getType());
	return "/complain/dispatching/add";
}

@RequestMapping("/saveData")
@ResponseBody
public Pagination saveData(Pagination page,SysComplain sysComplain){
	try{
		SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
		sysComplain.setComplainDelcomp(user.getId());
		sysComplain.setComplainDatetime(new Date());
		sysComplain.setUpdUser(user.getId());
		sysComplain.setUpdDatetime(new Date());
		sysComplainManager.save(sysComplain);
		page.setSuccess(true);
	}catch(Exception e){
		log.equals("申投诉信息保存出错");
		page.setSuccess(false);
		e.printStackTrace();
	}
	return page;
}

/**
 * @category  跳转到生产企业 申投诉编辑页面
 * @author 韩守松
 * @date   2015年9月8日
 * @param  @return
 */
@RequestMapping("/toSysComplainEditJsp")
public String toSysComplainEditJsp(){
	return "/complain/dispatching/edit";
}

/**
 * @category  跳转到生产企业 申投诉反馈页面
 * @author 韩守松
 * @date   2015年9月8日
 * @param  @return
 */
@SuppressWarnings("rawtypes")
@RequestMapping("/toSysComplainFeedBackJsp")
public String toSysComplainFeedBackJsp(String complainId){
	List list =  sysComplainDelcompManager.getSysComplainById(complainId);
	request.setAttribute("list", list.get(0));
	List listProj = sysComplainProjManager.getSysComplainProjById(complainId);
	request.setAttribute("listProj", listProj);
	List listHosp = sysComplainHospManager.getSysComplainHospById(complainId);
	request.setAttribute("listHosp", listHosp);
	List listProdcomp = sysComplainProdcompManager.getSysComplainProdcompById(complainId);
	request.setAttribute("listProdcomp", listProdcomp);
	List listProd = sysComplainProdManager.getSysComplainProdById(complainId);
	request.setAttribute("listProd", listProd);
	List listAttachment = sysComplainAttachmentManager.getSysComplainById(complainId);
	request.setAttribute("listAttachment", listAttachment);
	return "/complain/dispatching/feedback";
}

/**
 * @category  申投诉 反馈
 * @author 韩守松
 * @date   2015年9月14日
 * @param  @return
 */
@RequestMapping("/updataSysComplainStatus")
@ResponseBody
public Pagination updataSysComplainStatus(SysComplain sysComplain){
	Pagination page = new Pagination(request);
	try{
		SysUser user =  (SysUser)request.getSession().getAttribute(Constants.USERINFO);
		sysComplain.setUserType(user.getType());
		sysComplain.setUpdUser(user.getId());
		sysComplain.setUpdDatetime(new Date());
	    sysComplainManager.updataStatus(sysComplain);
		page.setSuccess(true);
	}catch(Exception e){
		log.equals("更新申投诉信息失败"+e);
		page.setSuccess(false);
		e.printStackTrace();
	}
	return page;
}

/**
 * @category  申投诉 反馈
 * @author 韩守松
 * @date   2015年9月14日
 * @param  @return
 */
@RequestMapping("/updataSysComplain")
@ResponseBody
public Pagination updataSysComplain(SysComplain sysComplain){
	Pagination page = new Pagination(request);
	try{
		SysUser user =  (SysUser)request.getSession().getAttribute(Constants.USERINFO);
		sysComplain.setUserType(user.getType());
		sysComplain.setFeedbackUsername(user.getId());
		sysComplain.setFeedbackDatetime(new Date());
		sysComplain.setUpdUser(user.getId());
		sysComplain.setUpdDatetime(new Date());
	    sysComplainManager.updata(sysComplain);
		page.setSuccess(true);
	}catch(Exception e){
		log.equals("更新申投诉信息失败"+e);
		page.setSuccess(false);
		e.printStackTrace();
	}
	return page;
}
}
